package com.ganzalang.gmall.designpatterns.composite2;

/**
 * @description:
 * @author: Ganzalang
 * @Date 2022/4/11 17:39
 */
public class Client {
    public static void main(String[] args) {
        //创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        //创建一个树枝构件
        Composite branch = new Composite();
        //创建一个叶子节点
        Leaf leaf = new Leaf();
        //建立整体
        root.add(branch);
        branch.add(leaf);
        display(root);
    }

    //通过递归遍历树
    public static void display(Composite root){
        for (Object child : root.getChildren()) {
            if(child instanceof Leaf){ //叶子节点
                Leaf l = (Leaf) child;
                l.doSomething();
            }else{ //树枝节点
                display((Composite)child);
            }
        }
    }
}
